﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ordinapoche
{
    /// <summary>
    ///     The representation of the memory of an Ordinapoche.
    /// </summary>

    public class Memory
    {
        private int[] _Data = new int[100];

        public Memory ()
        {
            _Data[0] = 1;
        }

        public virtual int this[int aCell]
        {
            get
            {
                return _Data[aCell];
            }
            set
            {
                if (aCell == 0)
                {
                    //  We silently ignore writes to memory cell 0.
                    return;
                }

                //  Writes are silently truncated to 3 digits.
                int v = value % 1000;

                if (_Data[aCell] != v)
                {
                    MemoryChangeEventArgs e = new MemoryChangeEventArgs (aCell, _Data[aCell], v);
                    OnDataChanging (e);
                    _Data[aCell] = v;
                    OnDataChanged (e);
                }
            }
        }

        public event EventHandler<MemoryChangeEventArgs> DataChanging;
        public event EventHandler<MemoryChangeEventArgs> DataChanged;

        protected virtual void OnDataChanging (MemoryChangeEventArgs e)
        {
            if (DataChanging != null)
            {
                DataChanging (this, e);
            }
        }

        protected virtual void OnDataChanged (MemoryChangeEventArgs e)
        {
            if (DataChanged != null)
            {
                DataChanged (this, e);
            }
        }
    }
}
